package cn.lotusnut.rabbitmqboot.configuration;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author JiangMing Zhou
 * @Date 2022/12/2 14:54
 * @Version 1.8
 *
 * 秒杀功能MQ配置
 *
 */
@Configuration
public class KillConfiguration {

    /**
     * 秒杀交换机、队列
     */


    @Bean
    public Queue killQueue(){
        Map<String,Object> initParam = new HashMap<>(2);
        initParam.put("x-max-length", 100);
        //设置消息溢出时如何处理（drop-head表示删除消息，当做死信处理，reject-publish表示拒绝接收消息）
        initParam.put("x-overflow", "drop-head");
        //绑定死信交换机
        initParam.put("x-dead-letter-exchange", "kill_dead_exchange");
        //死信路由
        initParam.put("x-dead-letter-routing-key", "dead_kill");
        return new Queue("kill_queue",true,false,false,initParam);
    }

    @Bean
    public DirectExchange killExchange(){
        return new DirectExchange("kill_exchange");
    }

    @Bean
    public Binding bindingKillQueueToKillExchange(Queue killQueue,DirectExchange killExchange){
        return BindingBuilder.bind(killQueue).to(killExchange).with("kill");
    }

    /**
     * 秒杀死信交换机，队列配置
     */

    @Bean
    public Queue killDeadQueue(){
        return new Queue("kill_dead_queue");
    }

    @Bean
    public CustomExchange killDeadExchange(){
        Map<String,Object> initParam = new HashMap<>(2);
        initParam.put("x-delayed-type", "direct");
        return new CustomExchange("kill_dead_exchange","x-delayed-message", true, false, initParam);
    }

    @Bean
    public Binding bindingKillDeadQueueToKillDeadExchange(Queue killDeadQueue,CustomExchange killDeadExchange){
        return BindingBuilder.bind(killDeadQueue).to(killDeadExchange).with("dead_kill").noargs();
    }

}
